package com.magicland;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class PlayerManager {

	private static Player pl;
	private final String fileName = "playerinfo.txt";
	
	public PlayerManager() {
		 
		if(pl == null){
			pl = new Player();
			
			try {
				loadPlayer();
			} catch (IOException e) {
				 
				e.printStackTrace();
			}
		}
	}
	
	
	public void loadPlayer() throws IOException{
		
		FileReader fr = new FileReader(fileName);
		BufferedReader br = new BufferedReader(fr);
		
		
		//nome
		pl.setNome(br.readLine());
		//diamante
		pl.setDiamante(Integer.valueOf(br.readLine()));
		
		//ouro
		pl.setOuro(Integer.valueOf(br.readLine()));
		
		//energia
		pl.setEnergia(Integer.valueOf(br.readLine()));
		
		//mercadoria
		pl.setMercadoria(Integer.valueOf(br.readLine()));
		
		//nivel
		pl.setNivel(Integer.valueOf(br.readLine()));
		
		//xp
		pl.setXp(Integer.valueOf(br.readLine()));

		br.close();

	}
	

	public void savePlayer() throws IOException{
	
		FileWriter fw = new FileWriter("texto", true);
		BufferedWriter bw = new BufferedWriter(fw);
	
	
		//nome
		bw.write(pl.getNome());
		//diamante
		bw.write(pl.getDiamante());
		
		//ouro
		bw.write(pl.getOuro());
		
		//energia
		bw.write(pl.getEnergia());
		
		//mercadoria
		bw.write(pl.getMercadoria());
		
		//nivel
		bw.write(pl.getNivel());
		
		//xp
		bw.write(pl.getXp());
	
		bw.close();

}



	public Player getPlayer(){
		return pl;
	}
	

}
